package com.hc.conferencecheck.interceptors;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hc.conferencecheck.entity.ResultMsg;
import com.hc.conferencecheck.entity.ResultStatusCode;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import sun.net.www.http.HttpClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 自定义拦截器，用于前台登录验证，页面跳转
 */
public class RouterInterceptor implements HandlerInterceptor {
    //1.使用默认的配置的httpclient
    CloseableHttpClient client = HttpClients.createDefault();
    private final String baseUrl = "http://localhost:8088";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println(">>>RouterInterceptor>>>>>>>在请求处理之前进行调用=======路由过滤器========");
        //获取请求路径
        String requestURL = request.getRequestURL().toString();

        //用户申请会议、签到、我的会议时必须先登录
        if (requestURL.contains("/apply")
                || requestURL.contains("/sign")
                || requestURL.contains("/checking")
                || requestURL.contains("/successReset")
                || requestURL.contains("/successRegister")
                || requestURL.contains("/myApply")
                || requestURL.contains("/items")
                ) {
            //如果包含指定请求路径，进行登录验证，如果验证通过就放行，否则return false;
            HttpSession session = request.getSession();
            Object user = null;
            if (requestURL.contains("/manage")) {
                //判断管理用户是否已登录
                user = session.getAttribute("adminUser");
            } else {
                user = session.getAttribute("user");
            }
            //进行跳转
            if (user == null) {
                //没有cookie,说明用户还没登录，跳转到登录页面
                HttpGet httpGet = new HttpGet(baseUrl + "login");
                client.execute(httpGet);
                return false;
            } else {
                //截取最后一个斜线后面的访问路径
                String routerPath = requestURL.substring(requestURL.lastIndexOf("/") + 1);
                HttpGet httpGet = new HttpGet(baseUrl + routerPath);
                client.execute(httpGet);
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
